package com.books4sale.admin.dto.tools;

import java.util.Date;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.net.ssl.KeyManager;

import com.books4sale.admin.dto.Book;
import com.books4sale.admin.dto.Profile;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public final class Helper {

	public final static String BOOK_NAME = "bookName";
	public final static String BOOK_COLLAGE = "bookCollage";
	public final static String BOOK_SUBJECT = "bookSubject";
	public final static String BOOK_PRICE = "bookPrice";
	public final static String BOOK_ID = "id";
	public final static String ROW_INDEX = "rowIndex";
	public final static int PAGE_CAPACITY = 14;

	// profile info:
	public final static String SHOP_ADDRESS = "address";
	public final static String SHOP_CITY = "city";
	public final static String SHOP_NAME = "name";
	public final static String SHOP_PHONE = "phone";

	private final static Helper helper = new Helper();

	private UserService userService;
	private User currentUser;

	private Date currentDate;

	private Helper() {
	}

	public static Helper get() {
		return helper;
	}

	public User getCurrentUser() {
		userService = UserServiceFactory.getUserService();
		currentUser = userService.getCurrentUser();
		return currentUser;
	}

	public Date getCurrentDate() {
		return new Date();
	}

	public Book insertBook(Book book) {
		System.out.println("book name: " + book.getBookName());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile p = pm.getObjectById(Profile.class, key);
		System.out.println("!>>>>>>>>>>>>>>>>>>>>>>>");
		p.getBooks().add(book);
		System.out.println("/////////////////////");
		try {
			pm.makePersistent(p);
		} finally {
			pm.close();
		}
		return book;

	}

	public Book editBook(Book book) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile p = pm.getObjectById(Profile.class, key);
		p.getBooks().add(book);
		try {
			pm.makePersistent(p);
		} finally {
			pm.close();
		}
		return book;
	}

	public List<Book> getAllBooks() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile userProfile = pm.getObjectById(Profile.class, key);
		return userProfile.getBooks();
	}

	public List<Book> getPageData(int pageNumber) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile userProfile = pm.getObjectById(Profile.class, key);
		int fromIndex = PAGE_CAPACITY * (pageNumber - 1);
		int toIndex = pageNumber * PAGE_CAPACITY - 1;
		if (toIndex > userProfile.getBooks().size()) {
			toIndex = userProfile.getBooks().size();
		}
		System.out.println("size: " + userProfile.getBooks().size());
		System.out.println("from: " + fromIndex + " to: " + toIndex);
		return userProfile.getBooks().subList(fromIndex, toIndex);
	}

	public void createNewProfile() {
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile newProfile = new Profile(key, "", "", "", "", getCurrentUser()
				.getEmail());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(newProfile);
		} finally {
			pm.close();
		}
	}

	// is user exists
	public Profile isUserProfileExists() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key userKey = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile profile = null;
		try {
			profile = pm.getObjectById(Profile.class, userKey);
		} catch (JDOObjectNotFoundException e) {
			System.out.println("user not found!");
		} finally {
			pm.close();
		}
		return profile;
	}

	public String getLogoutLink(String destinationURL) {
		UserService userService = UserServiceFactory.getUserService();
		return userService.createLogoutURL(destinationURL);
	}

	public Boolean deleteBook(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(Profile.class.getSimpleName(),
				getCurrentUser().getEmail());
		Profile p = pm.getObjectById(Profile.class, key);

		KeyFactory.Builder builder = new KeyFactory.Builder(Profile.class
				.getSimpleName(), getCurrentUser().getEmail());
		builder.addChild(Book.class.getSimpleName(), id);
		Book b = pm.getObjectById(Book.class, builder.getKey());
		b.setId(builder.getKey());
		try {
			pm.deletePersistent(b);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			pm.close();
		}

		return true;

	}
}
